package com.syy.spider.controller;

import com.syy.spider.dao.PyqMapper;
import com.syy.spider.dao.SpiderMapper;
import com.syy.spider.pojo.Picture;
import com.syy.spider.pojo.Pyq;
import com.syy.spider.pojo.User;
import com.syy.spider.service.PyqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
public class PyqController {
    @Autowired
    private PyqMapper pyqMapper;
    @Autowired
    private PyqService pyqService;
    @Autowired
    private SpiderMapper spiderMapper;

    @RequestMapping("/share")
    public String share(){
        return "pages/pyq";
    }
    @RequestMapping("/toAddPyq")
    public String toAddPyq(){
        return "pages/addPyq";
    }

    @RequestMapping("/getPyq")
    @ResponseBody
    public Picture getPyq(@RequestBody Map<String, Object> params){
        int picId = Integer.parseInt((String) params.get("pyqId"));
        return spiderMapper.selectPictureById(picId);
    }

    @RequestMapping("/addPyq")
    @ResponseBody
    public int addPyq(HttpServletRequest request, @RequestBody Map<String, Object> params){
        User user = (User) request.getSession().getAttribute("user");
        String username = user.getUsername();
        int id = (int) params.get("id");
        Picture picture = spiderMapper.selectPictureById(id);
        String url = picture.getJustImgUrl();
        String title = (String)params.get("title");
        String con = (String)params.get("con");
        String date = (String)params.get("date");
        return pyqService.insertPyq(title,con,url,username,date);
    }

    @RequestMapping("/getPyqs")
    @ResponseBody
    public List<Pyq> getPyqs(){
        return pyqMapper.selectAll();
    }

    @RequestMapping("/addPyqToPicture")
    @ResponseBody
    public int addPyqToPicture(HttpServletRequest request, @RequestBody Map<String, Object> params){
        LinkedHashMap<String, String> a = (LinkedHashMap<String, String>) params.get("pyq");
        String url = a.get("url");
        String title = a.get("title");
        String con = a.get("con");
        Picture picture = new Picture();
        User user = (User) request.getSession().getAttribute("user");
        picture.setTitle(title);
        picture.setDescription(con);
        picture.setJustImgUrl(url);
        picture.setUsername(user.getUsername());
        picture.setUrl("这个图片是收藏的别人的哦~");
        picture.setTitle("来自别人发布");
        picture.setSpiderTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        int b = spiderMapper.insertPicInfo(picture);
        try {
            spiderMapper.insertTitle(user.getUsername(),"来自别人发布");
        } catch (Exception e) {
            System.out.println("重复主题忽略！");
        }
        System.out.println(b);
        return b;
    }

    @RequestMapping("/dianzan")
    @ResponseBody
    public int dianzan(@RequestBody Map<String, Object> params){
        int id = (int) params.get("id");
        return pyqMapper.dianzan(id);
    }

    @RequestMapping("/cai")
    @ResponseBody
    public int cai(@RequestBody Map<String, Object> params){
        int id = (int) params.get("id");
        return pyqMapper.cai(id);
    }

    @RequestMapping("/getPyqsOrderByZan")
    @ResponseBody
    public List<Pyq> getPyqsOrderByZan(){
        return pyqMapper.selectOrderByZan();
    }
}
